package com.topsmob.piepline;

import com.alibaba.fastjson.JSONObject;
import com.topsmob.StartApp;
import com.topsmob.tool.*;
import org.apache.commons.lang.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * com.topsmob
 * Description:
 * User:李耀华
 * Date:2017-06-19.10:36
 */
public class JeeCmsPiePline implements Pipeline {
    private  Spider spider;
    private  boolean isFirst = true;
    private String lastId =  null;
    Properties properties = new Properties();
    String appId = "4367791046162477";
    String appKey = "RLhgj6VCsvy13qAiUHdb1SrH2ipX2W3R";
    String aesKey = "V8qyl8CNF7PkPwtT";
    String ivKey = "Renf9Nkf8EX6QuOT";
    public static String sessionKey = null;
    public boolean going = false;
    String nonce_str= RandomStringUtils.random(16, Num62.N62_CHARS);
    public Logger logger =  LoggerFactory.getLogger(getClass());
    public JeeCmsPiePline(Spider spider){
        getSessionKey();
        going = true;
        this.spider = spider;
        this.isFirst = true;
        try {

            logger.info("res路径:"+System.getProperty("user.dir")+ File.separator+"res.properties");
            InputStream inputStream = new FileInputStream(System.getProperty("user.dir")+ File.separator+"res.properties");
            properties.load(inputStream);
            inputStream.close();
            lastId = properties.getProperty("lastId");
            logger.info("最后一次:"+lastId);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void process(ResultItems resultItems, Task task) {
        //最后一条的话 停止爬虫
        if(resultItems.get("postId").toString().equals(lastId)){
            spider.stop();
            going = false;
            logger.info("存在记录停止爬虫");
        }
        if(going){
            try {
                Map<String,String> map = new HashMap();
                map.put("siteId","1");
                map.put("channelId","104");
                map.put("title",resultItems.get("title").toString());
                String content = ImageUtils.copyTxtHmtlImg(resultItems.get("content").toString(),
                        StartApp.siteUrl);
                map.put("txt",content);
                map.put("copyImg","true");
                map.put("author","某网友");
                map.put("contentStatus","2");
       /*  List<String> tagList = resultItems.get("label");
       String tags = "";
        for(String str:tagList){
            tags += str+",";
        }
        map.put("tags",tags);*/


                map.put("appId",appId);
                map.put("nonce_str",nonce_str);
                map.put("sessionKey",getSessionKey());
                String sign= PayUtil.createSign(map, appKey);
                map.put("sign",sign);
                System.out.println("&appId="+appId+"&nonce_str="+nonce_str+"&sessionKey="+getSessionKey()+"&sign="+sign);
                String str = HttpClientUtils.doPost("http://www.codingsearch.com/api/content/save.jspx",map,"utf-8");

                JSONObject jsonObject = JSONObject.parseObject(str);

                if(isFirst && jsonObject.get("status").equals("true")){
                    //写Properties文件

                    try {
                        FileOutputStream oFile = new FileOutputStream(System.getProperty("user.dir")+ File.separator+"res.properties", false);
                        properties.put("lastId",resultItems.get("postId").toString());
                        properties.store(oFile,"修改最后一条记录的Id");
                        oFile.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    isFirst = false;
                }


            } catch (Exception e) {

            }

        }else{
            logger.info("爬虫结束了！！！！！！！！！！！！！！！！！！！");
        }

    }

    private String getSessionKey(){
        if(sessionKey != null){
            return sessionKey;
        }else{
            try{
                HashMap<String,String> map = new HashMap<String, String>();
                map.put("username","robot1");
                map.put("aesPassword", AES128Util.encrypt("wenli319", aesKey,ivKey));
                map.put("appId",appId);
                map.put("nonce_str",nonce_str);
                String sign=PayUtil.createSign(map, appKey);
                map.put("sign",sign);
                logger.info("开始请求Session");
                String res = HttpClientUtils.doPost("http://www.codingsearch.com/api/user/login.jspx",map,"utf-8");
                JSONObject json = JSONObject.parseObject(res);
                sessionKey=(String) json.get("body");
                logger.info("sessionKey==>"+sessionKey);
            }catch (Exception e){
                logger.info("请求错误:"+e.getLocalizedMessage());
                sessionKey = null;
            }
        }


        return sessionKey;
    }
}
